package xxl.robotlegs.framework.impl 
{
	import xxl.robotlegs.framework.api.IContextSignal;
	import xxl.signals.api.IPrioritySignal;
	import xxl.signals.api.ISlot;
	import xxl.signals.impl.DeluxeSignal;
	import xxl.signals.impl.MapSignal;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class ContextSignal extends MapSignal implements IContextSignal
	{
		protected var _target:Object;
		
		public function ContextSignal(target:Object, ...valueClasses) 
		{
			_target = target;
			super(DeluxeSignal, valueClasses);
		}
		
		override protected function registerListenerToMap(key:Object, listener:Function, once:Boolean = false):ISlot 
		{
			const signal:DeluxeSignal = getSignalByKey(key) as DeluxeSignal;
			signal.target = _target;
			var slot:ISlot;
			if (once)
			{
				slot = signal.addOnce(listener);
			}
			else
			{
				slot = signal.add(listener);
			}
			slot.safelyExecute = true;
			return slot;
		}
		
		protected function registerListenerToMapWithPriority(key:Object, listener:Function, once:Boolean = false, priority:int = 0):ISlot
		{
			const signal:DeluxeSignal = getSignalByKey(key) as DeluxeSignal;
			signal.target = _target;
			var slot:ISlot;
			if (once)
			{
				slot = signal.addOnceWithPriority(listener, priority);
			}
			else
			{
				slot = signal.addWithPriority(listener, priority);
			}
			slot.safelyExecute = true;
			return slot;
		}
		
		/* INTERFACE xxl.robotlegs.framework.api.IContextSignal */
		
		public function addToMapWithPriority(key:Object, listener:Function, priority:int = 0):ISlot 
		{
			return registerListenerToMapWithPriority(key, listener, false, priority);
		}
		
		public function addOnceToMapWithPriority(key:Object, listener:Function, priority:int = 0):ISlot 
		{
			return registerListenerToMapWithPriority(key, listener, true, priority);
		}
		
		/* INTERFACE xxl.signals.api.IDeluxeSignal */
		
		public function addWithPriority(listener:Function, priority:int = 0):ISlot 
		{
			return registerListenerToMapWithPriority(defaultKey, listener, false, priority);
		}
		
		public function addOnceWithPriority(listener:Function, priority:int = 0):ISlot 
		{
			return registerListenerToMapWithPriority(defaultKey, listener, true, priority);
		}
		
	}

}